<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">自动化测试</h3>
      <el-form-item prop="userAccount">
        <el-input v-model="loginForm.userAccount" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix>
            <svg-icon icon-class="user" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="userPassword">
        <el-input v-model="loginForm.userPassword" type="password" size="large" auto-complete="off" placeholder="密码"
          @keyup.enter="handleLogin">
          <template #prefix>
            <svg-icon icon-class="userPassword" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <div class="user">
        <div>
          <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        </div>
      </div>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
    </div>
  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'
import { ElMessage } from 'element-plus'
const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  userAccount: "",
  userPassword: "",
  remember: false,
});

const loginRules = {
  userAccount: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  userPassword: [{ required: true, trigger: "blur", message: "请输入您的密码" }]
};

const loading = ref(false);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.remember) {
        Cookies.set("userAccount", loginForm.value.userAccount, { expires: 30 }); //30天后失效
        Cookies.set("userPassword", encrypt(loginForm.value.userPassword), { expires: 30 });
        Cookies.set("remember", loginForm.value.remember, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("userAccount");
        Cookies.remove("userPassword");
        Cookies.remove("remember");
      }
      userStore.login(loginForm.value).then((res) => {
        if (res) {
          router.push("/");
        }
        loading.value = false;
      }).catch(() => {
        loading.value = false;
      });
    }
  });
}

function getCookie() {
  const userAccount = Cookies.get("userAccount");
  const userPassword = Cookies.get("userPassword");
  const remember = Cookies.get("remember");
  loginForm.value = {
    userAccount: userAccount === undefined ? loginForm.value.userAccount : userAccount,
    userPassword: userPassword === undefined ? loginForm.value.userPassword : decrypt(userPassword),
    remember: remember === undefined ? false : Boolean(remember)
  };
}
getCookie();
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }

  .user {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .register {
      font-size: 12px;

      &:hover {
        cursor: pointer;
      }
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
